import { app } from 'electron'
import * as actions from '../actions/file'
import { userSetting } from '../actions/marktext'
import { isOsx } from '../../config'
import { COMMANDS } from '../../commands'

export default function (keybindings, userPreference, recentlyUsedFiles, language) {
  const { autoSave } = userPreference.getAll()
  const fileMenu = {
    label: language.getString('file'),
    submenu: [{
      label: language.getString('newTab'),
      accelerator: keybindings.getAccelerator(COMMANDS.FILE_NEW_TAB),
      click (menuItem, browserWindow) {
        actions.newBlankTab(browserWindow)
      }
    }, {
      label: language.getString('newWindow'),
      accelerator: keybindings.getAccelerator(COMMANDS.FILE_NEW_WINDOW),
      click (menuItem, browserWindow) {
        actions.newEditorWindow()
      }
    }, {
      type: 'separator'
    }, {
      label: language.getString('openFile'),
      accelerator: keybindings.getAccelerator(COMMANDS.FILE_OPEN_FILE),
      click (menuItem, browserWindow) {
        actions.openFile(browserWindow)
      }
    }, {
      label: language.getString('openFolder'),
      accelerator: keybindings.getAccelerator(COMMANDS.FILE_OPEN_FOLDER),
      click (menuItem, browserWindow) {
        actions.openFolder(browserWindow)
      }
    }]
  }

  if (!isOsx) {
    const recentlyUsedMenu = {
      label: language.getString('openRecent'),
      submenu: []
    }

    for (const item of recentlyUsedFiles) {
      recentlyUsedMenu.submenu.push({
        label: item,
        click (menuItem, browserWindow) {
          actions.openFileOrFolder(browserWindow, menuItem.label)
        }
      })
    }

    recentlyUsedMenu.submenu.push({
      type: 'separator',
      visible: recentlyUsedFiles.length > 0
    }, {
      label: language.getString('clearRecentlyUsed'),
      enabled: recentlyUsedFiles.length > 0,
      click (menuItem, browserWindow) {
        actions.clearRecentlyUsed()
      }
    })
    fileMenu.submenu.push(recentlyUsedMenu)
  } else {
    fileMenu.submenu.push({
      role: 'recentdocuments',
      submenu: [
        {
          role: 'clearrecentdocuments'
        }
      ]
    })
  }

  fileMenu.submenu.push({
    type: 'separator'
  }, {
    label: language.getString('save'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_SAVE),
    click (menuItem, browserWindow) {
      actions.save(browserWindow)
    }
  }, {
    label: language.getString('saveAs'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_SAVE_AS),
    click (menuItem, browserWindow) {
      actions.saveAs(browserWindow)
    }
  }, {
    label: language.getString('autoSave'),
    type: 'checkbox',
    checked: autoSave,
    id: 'autoSaveMenuItem',
    click (menuItem, browserWindow) {
      actions.autoSave(menuItem, browserWindow)
    }
  }, {
    type: 'separator'
  }, {
    label: language.getString('moveTo'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_MOVE_TO),
    click (menuItem, browserWindow) {
      actions.moveTo(browserWindow)
    }
  }, {
    label: language.getString('rename'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_RENAME),
    click (menuItem, browserWindow) {
      actions.rename(browserWindow)
    }
  }, {
    type: 'separator'
  }, {
    label: language.getString('import'),
    click (menuItem, browserWindow) {
      actions.importFile(browserWindow)
    }
  }, {
    label: language.getString('export'),
    submenu: [
      {
        label: language.getString('html'),
        click (menuItem, browserWindow) {
          actions.exportFile(browserWindow, 'styledHtml')
        }
      }, {
        label: language.getString('pdf'),
        click (menuItem, browserWindow) {
          actions.exportFile(browserWindow, 'pdf')
        }
      }
    ]
  }, {
    label: language.getString('print'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_PRINT),
    click (menuItem, browserWindow) {
      actions.printDocument(browserWindow)
    }
  }, {
    type: 'separator',
    visible: !isOsx
  }, {
    label: language.getString('preferences'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_PREFERENCES),
    visible: !isOsx,
    click () {
      userSetting()
    }
  }, {
    type: 'separator'
  }, {
    label: language.getString('closeTab'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_CLOSE_TAB),
    click (menuItem, browserWindow) {
      actions.closeTab(browserWindow)
    }
  }, {
    label: language.getString('closeWindow'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_CLOSE_WINDOW),
    click (menuItem, browserWindow) {
      actions.closeWindow(browserWindow)
    }
  }, {
    type: 'separator',
    visible: !isOsx
  }, {
    label: language.getString('quit'),
    accelerator: keybindings.getAccelerator(COMMANDS.FILE_QUIT),
    visible: !isOsx,
    click: app.quit
  })
  return fileMenu
}
